function ccR_Initialize()
	if not ccRF then
		ccRF = CreateFrame("Frame");
		ccRF:RegisterEvent("PLAYER_ENTERING_WORLD")
		ccRF:RegisterEvent("ADDON_LOADED")
		ccRF:RegisterEvent("LFG_PROPOSAL_SHOW")
		ccRF:RegisterEvent("LOOT_OPENED")
		ccRF:RegisterEvent("LOOT_CLOSED")
		ccRF:RegisterEvent("ZONE_CHANGED")
		ccRF:RegisterEvent("PLAYER_TARGET_CHANGED")
		ccRF:SetScript("OnEvent", ccR_OnEvent)
	end
end

ccRV_CurrentTargetName = nil
function ccR_OnEvent(self, event, arg1)
	--[[
		Target Tracking
	]]
	if event == "PLAYER_TARGET_CHANGED"
	and ccRS.CF_Target_Tracking
	and PlayerIsSOLO()
	then
		if ccrTargetName() ~= nil 
		and ccrTargetName() ~= ccRV_CurrentTargetName
		then
			-- Stop Tracking old target
			if ccRV_CurrentTargetName ~= nil and GetTrackingState(ccRV_CurrentTargetName) == true then TrackByName(ccRV_CurrentTargetName) end;
			-- Update the var
			ccRV_CurrentTargetName = ccrTargetName()
			-- Track the new name
			if GetTrackingState(ccRV_CurrentTargetName) == false then TrackByName(ccRV_CurrentTargetName) end;
		end
	end	
	--[[
		Auto Join LFD/LFR
	]]
	if event == "LFG_PROPOSAL_SHOW"
	and ccRS.CF_AutoJoinLFG == true
	then
		AcceptProposal();
	end
	--[[
		ccR Loads
	]]
	if event == "ADDON_LOADED" then
		ccRS = CopyDefaults(ccR_Default, ccRS)
		PrintToChat("Loaded")

		local vTime = ccRS.TimeCountMax
		local vObjectTime = ccRS.ObjectListUpdate
		local vAutoLootTime = 0.50

		ccrTimer.Register('ccR_RunRotation', ccR_RunRotation, vTime)
		ccrTimer.Register('ccR_GatherObjectList', ccR_GatherObjectList, vObjectTime)
		if ccRS.Auto_Loot then ccrTimer.Register('ccR_AutoLoot', ccR_AutoLoot, vAutoLootTime) end

		--[[
			PASSIVE PLAYER ABILITIES
		]]
		if ccrClass() == "Rogue" then
			-- Auto Stealth
			if ccRS.Rogue_UseAutoStealth then ccrTimer.Register('ccR_Rogue_UseAutoStealth', ccR_Rogue_UseStealth, 0.5) else ccrTimer.Unregister('ccR_Rogue_UseAutoStealth') end
		elseif ccrClass() == "Hunter" then
			if ccRS.Hunter_UseHeartOfThePhoenix then ccrTimer.Register('ccR_Hunter_Heart_of_the_Phoenix', ccR_Hunter_Heart_of_the_Phoenix, 0.5) else ccrTimer.Unregister('ccR_Hunter_Heart_of_the_Phoenix') end
			if ccRS.Hunter_UseAutoAspect then ccrTimer.Register('ccR_Hunter_Auto_Aspect', ccR_Hunter_Auto_Aspect, 0.5) else ccrTimer.Unregister('ccR_Hunter_Auto_Aspect') end
			if ccRS.Hunter_UseAutoPetSpec then ccrTimer.Register('ccR_Hunter_Auto_Pet_Spec', ccR_Hunter_Auto_Pet_Spec, 0.5) else ccrTimer.Unregister('ccR_Hunter_Auto_Pet_Spec') end
			if ccRS.Hunter_UseAutoPetGrowl then ccrTimer.Register('ccR_Hunter_Auto_Pet_Growl', ccR_Hunter_Auto_Pet_Growl, 0.5) else ccrTimer.Unregister('ccR_Hunter_Auto_Pet_Growl') end
		elseif ccrClass() == "Death Knight" then
			if ccRS.DeathKnight_UseDeathsAdvance then ccrTimer.Register('ccR_DeathKnight_DeathsAdvance', ccR_DeathKnight_DeathsAdvance, 0.5) else ccrTimer.Unregister('ccR_DeathKnight_DeathsAdvance') end
		end
		--
		--[[
			Custom Features 
		]]
		-- Use Track Good Lewt
		if ccRS.CF_Track_Good_Lewt then ccrTimer.Register('ccR_Track_Good_Lewt',ccR_Track_Good_Lewt, 0.1) end

		-- Unregister this event
		ccRF:UnregisterEvent("ADDON_LOADED")
	end
	--[[
		Login/Portal/rl
	]]
	if event == "PLAYER_ENTERING_WORLD" then
		if not groupcomposereditbox then GroupComposerFrame() end
		if not GStarRunning then AnimatedSetup() end -- new
		if not ccRS.ClassPlayer or ccRS.ClassPlayer ~= select(3, UnitClass("player")) then ccRS.ClassPlayer = select(3, UnitClass("player")) end
		-- Preventing causing WoW crashing on portaling
		if ccrStart then
			ccrStart = false 
			-- ccR:Hide()
			AnimatedMaint("offeverything")
		end;
	end
	--[[
		AUTO LOOT
	]]
	if ccRS.Auto_Loot then
		if event == "LOOT_OPENED" then
			bPauseAutoLoot = true
			vLootCount = GetNumLootItems()
			if vLootCount > 0 then
				for i = 1, vLootCount do
					local texture, item, quantity, quality, locked = GetLootSlotInfo(i)
					if locked ~= 1 then
						LootSlot(i);
						ConfirmLootSlot(i);
					end
				end
			end
			if vLootCount == 0 then bPauseAutoLoot = false end;
		end
		if event == "LOOT_CLOSED"
		and GetNumLootItems() == 0
		then
			bPauseAutoLoot = false
		end
	end
end

function ccR_RunRotation()
	-- Run once, Passive / Update checking / Custom features
	if not ccrRunOnce then
		if ccRS.CF_Treasure_Tracking then ccR_MoP_Treasure_Tracking() end;
		ccR_Custom_Features()
		--RequestPage("https://code.google.com/p/fh-ccr/wiki/UPDATECHECKER", ccR_Update)
		if ccRS.G1zStarTimelessIsle then print("TimelessIsleBuffs script loaded.") SetTimerCallback(G1zStarTimelessIsleBuffsUpTime,  50) ccRS.G1zStarTimelessIsle = true end
		if ccRS.G1zStarSlowfall then print("Slowfall script loaded.") SetTimerCallback(G1zStarSlowfallfunc, 50) ccRS.G1zStarSlowfall = true end
		GTargetListControl()
		ccrRunOnce = true
	end

  	if buttonyes and buttonyes:IsShown() then
  		-- ccR:Hide()
  		AnimatedMaint("offeverything")
  		ccrStart = false
  		return
  	end
  	-- Run Rotation
	if ccrStart then
		-- If player dies, Disable
		if UnitIsDeadOrGhost("player") then
			-- ccR:Hide()
			AnimatedMaint("offeverything")
			ccrStart = false
			return
		end
		ccR_PreRotation()
		-- ccR:Show()
		AnimatedMaint("oneverything")
	else
		-- ccR:Hide()
		AnimatedMaint("offeverything")
		ccrStart = false
	end
end

SLASH_CCRCMD1 = "/ccr";
SlashCmdList["CCRCMD"] = ccRCMD;
SLASH_CCRL1 = "/rl"
SlashCmdList["CCRL"] = ccReloadUI;
SLASH_CCRTR1 = "/tr"
SlashCmdList["CCRTR"] = ccR_Track;
SLASH_CCRPR1 = "/pr"
SlashCmdList["CCRPR"] = ccR_Script_Print;

ccR_Initialize()